package fr.greta.centreFormation.controller;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Hashtable;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import fr.greta.centreFormation.bean.Cursus;
import fr.greta.centreFormation.bean.UniteEnseignement;
import fr.greta.centreFormation.dao.Bdd;
import fr.greta.centreFormation.dao.CursusHome;
import fr.greta.centreFormation.dao.UniteEnseignementHome;

/**
 * Servlet implementation class ScriptModifCursus
 */
public class ScriptModifCursus extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ScriptModifCursus() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		////----------------------------------\\\\
		///		V�rification des donn�es	   \\\
		//--------------------------------------\\

		String intitule = request.getParameter("intitule");
		String CodeRome = request.getParameter("codeRome");
		String description = request.getParameter("description");
		String idDomaineString = request.getParameter("domaine");
		String[] lesUe = request.getParameterValues("lesUe");

		Integer idDomaine = Integer.parseInt(idDomaineString);

		// Si tableau de unite existe alors convertion

		Hashtable<Integer, String> lesUeDuCursus = new Hashtable<Integer, String>();
		if(lesUe != null){
			for(String ue : lesUe){
				Integer idUe = Integer.parseInt(ue);
				UniteEnseignement u = UniteEnseignementHome.selectUneUniteEnseignementById(idUe);
				lesUeDuCursus.put(u.getIdUniteEnseignement(), u.getIntituleUniteEnseignement());
			}
		}

		HttpSession session = request.getSession(true);

		if(intitule == "" || CodeRome == "" || description == "" || lesUe == null){

			System.out.println("manque donner");
			session.setAttribute("intitule", intitule);
			session.setAttribute("codeRome", CodeRome);
			session.setAttribute("description", description);
			session.setAttribute("lesUe", lesUeDuCursus);
			session.setAttribute("idDomaine", idDomaine);

			request.setAttribute("etat", "Certain champs n'ont pas été remplis.");

			request.getRequestDispatcher("formulaireCursus.jsp").forward(request, response);

		}else if(CodeRome.length() > 6){

			System.out.println("Code Rome trop long");
			session.setAttribute("intitule", intitule);
			session.setAttribute("codeRome", CodeRome);
			session.setAttribute("description", description);
			session.setAttribute("lesUe", lesUeDuCursus);
			session.setAttribute("idDomaine", idDomaine);

			request.setAttribute("etat", "Le Code rome ne doit pas dépacer 6 carratactaires.");
			request.getRequestDispatcher("formulaireCursus.jsp").forward(request, response);

		}else{

			////----------------------------------\\\\
			///		Insertion dans la base		   \\\
			//--------------------------------------\\

			Cursus leCursus = new Cursus();
			//R�cup�ration des unit� d'enseignement =>>
			ArrayList<UniteEnseignement> collectionDeUniteEnseignements = new ArrayList<UniteEnseignement>();

			for(String i : lesUe){
				Integer y = Integer.parseInt(i);
				UniteEnseignement ue = UniteEnseignementHome.selectUneUniteEnseignementById(y);
				collectionDeUniteEnseignements.add(ue);
			}

			leCursus.setIdCursus((Integer) session.getAttribute("idCursus"));
			leCursus.setIntituleCursus(intitule);
			leCursus.setCodeRomeCursus(CodeRome);
			leCursus.setDescriptionCursus(description);
			leCursus.setCollectiondeUE(collectionDeUniteEnseignements);

			try {
				System.out.println("resultat update => "+CursusHome.updateCursus(leCursus, idDomaine));
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			response.sendRedirect("ModifCursus");
			//request.getRequestDispatcher("ModifCursus").forward(request, response);


		}



	}

}
